﻿using AutoMapper;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Production_Modules;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Production_Modules
{
    public class ProduceUpdateHandler : IRequestHandler<ProduceUpdateCommand, ApiResult<int>>
    {
        IMapper _mapper;
        IRepository<ProduceModel> produceRep;
        public ProduceUpdateHandler(IMapper mapper, IRepository<ProduceModel> produceRep)
        {
            _mapper = mapper;
            this.produceRep = produceRep;
        }
        public async Task<ApiResult<int>> Handle(ProduceUpdateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            ProduceModel produce = _mapper.Map<ProduceModel>(request);
            produce.UpdateTime = DateTime.Now;
            var num = await produceRep.UpdateAsync(produce);

            if (num > 0)
            {
                result = new ApiResult<int>().Success("修改生产订单成功", num);
            }
            else
            {
                result = new ApiResult<int>().Fail("修改生产订单失败");
            }
            return await Task.FromResult(result);
        }
    }
}
